//
//  SonyPRS505.m
//  Portable Reader
//
//  Created by Chris Karr on 7/10/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "SonyPRS505.h"


@implementation SonyPRS505

- (id) init
{
	if (self = [super init])
	{
		self.name = NSLocalizedString (@"Sony Personal Reader System (PRS-505)", nil);
	}
	
	return self;
}

- (NSArray *) getFilesFromDatabaseXML:(NSXMLDocument *) xml
{
	NSMutableArray * installedFiles = [NSMutableArray array];
	
	NSXMLElement * root = [xml rootElement];
	
	NSArray * records = [root elementsForName:@"records"];
	
	for (NSXMLElement * record in records)
	{
		NSArray * texts = [record elementsForName:@"xs1:text"];
		
		for (NSXMLElement * text in texts)
		{
			NSMutableDictionary * file = [NSMutableDictionary dictionary];
			
			NSXMLNode * authorNode = [text attributeForName:@"author"];
			
			if (authorNode != nil)
				[file setValue:[authorNode stringValue] forKey:@"author"];
			
			NSXMLNode * titleNode = [text attributeForName:@"title"];
			
			if (titleNode != nil)
				[file setValue:[titleNode stringValue] forKey:@"title"];
			
			NSXMLNode * typeNode = [text attributeForName:@"mime"];
			
			if (typeNode != nil)
				[file setValue:[typeNode stringValue] forKey:@"type"];
			
			[installedFiles addObject:file];
		}
	}
	
	return installedFiles;
}	

- (NSArray *) getFilesFromCacheXML:(NSXMLDocument *) xml
{
	NSMutableArray * installedFiles = [NSMutableArray array];
	
	NSXMLElement * root = [xml rootElement];
	
	NSArray * texts = [root elementsForName:@"text"];
		
	for (NSXMLElement * text in texts)
	{
		NSMutableDictionary * file = [NSMutableDictionary dictionary];
			
		NSXMLNode * authorNode = [text attributeForName:@"author"];
			
		if (authorNode != nil)
			[file setValue:[authorNode stringValue] forKey:@"author"];
			
		NSXMLNode * titleNode = [text attributeForName:@"title"];
		
		if (titleNode != nil)
			[file setValue:[titleNode stringValue] forKey:@"title"];
			
		NSXMLNode * typeNode = [text attributeForName:@"mime"];
			
		if (typeNode != nil)
			[file setValue:[typeNode stringValue] forKey:@"type"];
			
		[installedFiles addObject:file];
	}
	
	return installedFiles;
}	

- (void) refreshInternal
{
	
	NSWorkspace * workspace = [NSWorkspace sharedWorkspace];
	NSFileManager * manager = [NSFileManager defaultManager];
	
	NSString * xmlPath = nil;
	
	NSArray * mounts = [workspace mountedRemovableMedia];
	
	for (NSString * path in mounts)
	{
		NSString * cachePath = [[[path stringByAppendingPathComponent:@"database"] stringByAppendingPathComponent:@"cache"]
								stringByAppendingPathComponent:@"media.xml"];
		
		if ([manager fileExistsAtPath:cachePath])
			xmlPath = cachePath;
	}
	
	if (xmlPath == nil)
	{
		// Error about no visible file.
		
		return;
	}
	
	NSError * error = nil;
	
	NSXMLDocument * xml = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:xmlPath]
															   options:0 error:&error];
	
	if (xml == nil)
	{
		// Error
		return;
	}

	[self.files addObjectsFromArray:[self getFilesFromDatabaseXML:xml]];
	
	[xml release];
}

	
- (void) refreshExternal
{
	
	NSWorkspace * workspace = [NSWorkspace sharedWorkspace];
	NSFileManager * manager = [NSFileManager defaultManager];
	
	NSString * xmlPath = nil;
	
	NSArray * mounts = [workspace mountedRemovableMedia];
	
	for (NSString * path in mounts)
	{
		NSString * cachePath = [[[path stringByAppendingPathComponent:@"Sony Reader"] stringByAppendingPathComponent:@"database"]
								stringByAppendingPathComponent:@"cache.xml"];
		
		if ([manager fileExistsAtPath:cachePath])
			xmlPath = cachePath;

		NSLog (@"path = %@", xmlPath);
	}
	
	if (xmlPath == nil)
	{
		// Error about no visible file.
		
		return;
	}
	
	NSError * error = nil;
	
	NSXMLDocument * xml = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:xmlPath]
															   options:0 error:&error];
	
	if (xml == nil)
	{
		NSLog (@"no xml");
		// Error
		return;
	}
	
	[self.files addObjectsFromArray:[self getFilesFromCacheXML:xml]];
	
	[xml release];
}
	
- (void) refresh
{
	[super refresh];
	
	[self.files removeAllObjects];

	[self refreshInternal];
	[self refreshExternal];
}

@end
